package com.tjetc.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjetc.common.JsonResult;
import com.tjetc.dao.CaregiverInfoMapper;
import com.tjetc.dao.ElderInfoMapper;
import com.tjetc.dao.EmployeeMapper;
import com.tjetc.entity.CaregiverInfo;
import com.tjetc.entity.Employee;
import com.tjetc.service.CaregiverInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CaregiverInfoServiceImpl implements CaregiverInfoService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private CaregiverInfoMapper caregiverInfoMapper;
    @Override
    public JsonResult findById(Long id) {
        CaregiverInfo caregiverInfo=caregiverInfoMapper.selectById(id);
        //selectById是mybatisplus提供的单表查询操作，生成sql语句都是mybatisplus内置
        Employee employee = employeeMapper.selectById(caregiverInfoMapper.selectEmployeeId(id));
        caregiverInfo.setEmployee(employee);
        return JsonResult.success(caregiverInfo);
    }
    @Override
    public JsonResult findPage(String name, Integer pageNo, Integer pageSize) {
        //设置页码和每页数量
        Page<CaregiverInfo> page=Page.of(pageNo,pageSize);
        //把adminPage传入mapper查询中，adminPage就是最后的分页结果
        //护工查所有分页，没有模糊查询
        Page<CaregiverInfo>caregiverInfoPage= caregiverInfoMapper.selectPageLikeEmployee(page,name);
        for (CaregiverInfo caregiverInfo : caregiverInfoPage.getRecords()) {
            Employee employee = employeeMapper.selectById(caregiverInfoMapper.selectEmployeeId(caregiverInfo.getCaregiverId()));
            caregiverInfo.setEmployee(employee);
        }
        //convert方法转换Page中 records集合元素类型 Admin---->AdminView
        //new Function<Admin,AdminView>(){};表示创建一个实现Function的匿名类 对象
        return JsonResult.success(caregiverInfoPage);
    }
    @Override
    public JsonResult deleteById(Long id) {
        int rows=caregiverInfoMapper.deleteById(id);
        if (rows>0){
            return JsonResult.success("删除成功");
        }
        else {
            return JsonResult.fail("删除失败");
        }
    }
    //@Transactional用在service方法上，保证事务
    //注意：默认情况@Transactional只对运行时异常起作用(RuntimeException)
    @Transactional
    @Override
    public JsonResult save(CaregiverInfo caregiverInfoParam) {
        CaregiverInfo caregiverInfo=null;
        LocalDateTime now=LocalDateTime.now();
        //通过判断id值有无判断是新增还是更新，id值无是新增，id值有就是更新
        if(caregiverInfoParam.getCaregiverId()==null||caregiverInfoParam.getCaregiverId()<=0){
            //新增
            caregiverInfo=new CaregiverInfo();
            BeanUtils.copyProperties(caregiverInfoParam,caregiverInfo);
            caregiverInfo.setCreateTime(now);
            caregiverInfo.setUpdateTime(now);
            caregiverInfoMapper.insert(caregiverInfo);
            return JsonResult.success("新增成功",null);
        }else{
            //更新
            //根据id查询Admin信息
            caregiverInfo=caregiverInfoMapper.selectById(caregiverInfoParam.getCaregiverId());
            if(caregiverInfo==null){
                return JsonResult.fail("用户不存在，更新失败");
            }
            caregiverInfo.setUpdateTime(now);
            caregiverInfo.setEmployee(caregiverInfoParam.getEmployee());
            caregiverInfo.setEmployeeId(caregiverInfoParam.getEmployeeId());
            caregiverInfo.setEvaluationScore(caregiverInfoParam.getEvaluationScore());
            caregiverInfo.setExperienceYears(caregiverInfoParam.getExperienceYears());
            caregiverInfo.setIsDeleted(caregiverInfoParam.getIsDeleted());
            caregiverInfo.setLastTrainingDate(caregiverInfoParam.getLastTrainingDate());
            caregiverInfo.setQualificationLevel(caregiverInfoParam.getQualificationLevel());
            caregiverInfo.setSpecialty(caregiverInfoParam.getSpecialty());
            caregiverInfo.setTrainedElderCount(caregiverInfoParam.getTrainedElderCount());
            int rows=caregiverInfoMapper.updateById(caregiverInfo);
            if (rows>0){
                return JsonResult.success("更新成功",null);
            }else {
                return JsonResult.fail("用户不存在，更新失败");
            }
        }
    }

    @Override
    public JsonResult findByEmployeeId(Long id) {
        CaregiverInfo caregiverInfo=caregiverInfoMapper.selectByEmployeeId(id);
        //selectById是mybatisplus提供的单表查询操作，生成sql语句都是mybatisplus内置
        Employee employee = employeeMapper.selectById(caregiverInfoMapper.selectEmployeeId(id));
        caregiverInfo.setEmployee(employee);
        return JsonResult.success(caregiverInfo);
    }
    @Override
    public JsonResult getCareGiverList(){
        List<Map<String, Object>> careGiverList =caregiverInfoMapper.getCareGiverList();
        return JsonResult.success(careGiverList);
    }
}
